package cn.nawang.ebeim.api.repository.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;

import cn.nawang.ebeim.api.comm.BaseQuery;
import cn.nawang.ebeim.api.comm.PageList;
import cn.nawang.ebeim.api.comm.Pages;
import cn.nawang.ebeim.api.domain.PushMessage;
import cn.nawang.ebeim.api.utils.ConvertUtil;

/**
 * @ClassName: PushMessageRepositoryImpl
 * @Description: TODO
 * @author fanyb
 * @date Oct 14, 2015 2:16:31 PM
 * @version v1.0
 */
public class PushMessageRepositoryImpl extends BaseQuery {

  @Autowired
  private EntityManagerFactory entityManagerFactory;

  public PageList findAllByPaging(Pages page) {
    String hqlCount =
        "select count(bl.id) from PushMessage bl,User u where u.id=bl.createUserId and 1=1 ";
    String hqlList = "select new cn.nawang.ebeim.api.domain.PushMessage(bl.id,bl.pushMessageTitle,"
        + "bl.pushMessageContent,bl.createTime,bl.createUserId,u.name)"
        + " from PushMessage bl,User u where u.id=bl.createUserId and 1=1 ";
    String order = " order by bl.id desc";
    PageList pl = null;
    try {
      String cod = joinSearchCondition(page);// 拼接查询条件
      pl = queryListByPaging(hqlCount + cod + order, hqlList + cod + order, page);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return pl;
  }

  public List<PushMessage> findMoreThan(int id) {
    String hqlList = "select new cn.nawang.ebeim.api.domain.PushMessage(bl.id,bl.pushMessageTitle,"
        + "bl.pushMessageContent,bl.createTime,bl.createUserId,u.name)"
        + " from PushMessage bl,User u where u.id=bl.createUserId ";
    String order = " order by bl.id desc";
    List<PushMessage> pl = null;
    Map<String, Object> paraMap = new HashMap<String, Object>();
    EntityManager em = entityManagerFactory.createEntityManager();
    try {
 /*     paraMap.put("id", id);*/
      Query query = em.createQuery(hqlList + order);
      for (Map.Entry<String, Object> map : paraMap.entrySet()) {
        query.setParameter(map.getKey(), map.getValue());
      }
      query.setMaxResults(10);
      pl = query.getResultList();
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
      em.close();
    }
    return pl;
  }

  public boolean delete(int[] ids) {
    try {
      String values = ConvertUtil.toDbString(ids);
      String hqlList = "delete from PushMessage where id in" + values;
      Map<String, Object> paraMap = new HashMap<String, Object>();
      boolean deleteFlag = this.execHQL(hqlList, paraMap);
      return deleteFlag;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

  public PushMessage findPushMessage(int id) {
    String hqlList = "select new cn.nawang.ebeim.api.domain.PushMessage(bl.id,bl.pushMessageTitle,"
        + "bl.pushMessageContent,bl.createTime,bl.createUserId,u.name)"
        + " from PushMessage bl,User u where u.id=bl.createUserId and bl.id=:id ";
    String order = " order by bl.id desc";
    List<PushMessage> pl = null;
    Map<String, Object> paraMap = new HashMap<String, Object>();
    PushMessage pushMessage = null;
    try {
      paraMap.put("id", id);
      pl = queryObject(hqlList + order, paraMap);
      if (!pl.isEmpty() && pl != null) {
        pushMessage = pl.get(0);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return pushMessage;
  }
}
